/* GUI CONVERTED from rcrsupc.p (converter v1.76) Sun May 19 19:42:56 2002 */
/* rcrsupc.p - Release Management Customer Schedules                          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*V8:ConvertMode=Maintenance                                                  */
/*K1Q4*/ /*V8:RunMode=Character,Windows                                       */
/* REVISION: 8.6    LAST MODIFIED: 09/20/96           BY: TSI *K005*          */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98           BY: *K1Q4* Alfred Tan   */
/* REVISION: 8.6    LAST MODIFIED: 11/09/98           BY: *K1Y4* Reetu Kapoor */
/* REVISION: 8.6    LAST MODIFIED: 03/11/99           BY: *K200* Poonam Bahl  */
/* REVISION: 8.6    LAST MODIFIED: 08/19/99           BY: *K229* Surekha Joshi*/
/* REVISION: 9.1    LAST MODIFIED: 11/16/99           BY: *K22Q* J. Fernando  */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00           BY: *N0KP* myb          */
/* REVISION:$       LAST MODIFIED: 05/20/02       BY: *P070* Lena Lim */
 /* REQUIRED SCHEDULE UPDATE SUBPROGRAM */

         {mfdeclre.i}

         /*INPUT PARAMETERS*/
         define input parameter sod_recid as recid no-undo.
         define input parameter new_rlse_id as character no-undo.
         /*OUTPUT PARAMETERS*/
         define output parameter undo_status as logical no-undo.

         /*LOCAL VARIABLES*/
         define variable act_prcnt_ship as integer no-undo.
         define variable new_prcnt_ship as integer no-undo.
         define variable shp_peg_qty as decimal no-undo.
         define variable ship_cmplt as decimal no-undo.
         define variable msgtext as character no-undo.
         define variable rss_schedule as integer initial 3 no-undo.
         define variable errlevel as integer no-undo.
         define variable errgen as logical no-undo.

         /*BUFFERS*/
         define buffer act_schd for schd_det.
         define buffer act_rqm for rqm_det.

         /*NEW SHARED STREAMS*/
         define new shared stream nullout.


         undo_status = true.

         find sod_det
           where recid(sod_det) = sod_recid
           no-lock.

         find so_mstr
           where so_nbr = sod_nbr
           no-lock.

         ship_cmplt = if so_ship_cmplt = 0
                         then 100 else so_ship_cmplt.

         /***********************************************************/
         /* FIND ACTIVE OPEN REQUIREMENTS THAT OVERLAP THE NEW RSS. */
         /***********************************************************/
         if not so_cum_acct then
         do:
            for each act_schd
               where act_schd.schd_type    = rss_schedule
                 and act_schd.schd_nbr     = sod_nbr
                 and act_schd.schd_line    = sod_line
                 and act_schd.schd_rlse_id = sod_curr_rlse_id[rss_schedule]
               exclusive-lock:
/*GUI*/ if global-beam-me-up then undo, leave.


/*K200*/       /* SKIPPING PLANNING DATA FROM CURRENT ACTIVE RSS */
/*K200*/       /* WHEN EXCLUDE PLANNING DATA WAS SET TO YES      */
/*K200*/       /* FOR THE SCHEDULED ORDER                        */

/*K200*/       if so_merge_rss and so__qadl02 and
/*K229** /*K200*/          act_schd.schd_fc_qual = "1" */
/*K229*/          trim(substring(act_schd.schd_fc_qual,1,1)) = "1"
/*K200*/       then
/*K200*/          next.

/*K1Y4*/       assign
/*K1Y4*/          act_prcnt_ship = integer((act_schd.schd_ship_qty /
/*K1Y4*/                                    act_schd.schd_discr_qty) * 100).

               find schd_det
                  where schd_det.schd_type      = act_schd.schd_type
                    and schd_det.schd_nbr       = act_schd.schd_nbr
                    and schd_det.schd_line      = act_schd.schd_line
                    and schd_det.schd_rlse_id   = new_rlse_id
                    and schd_det.schd_date      = act_schd.schd_date
                    and schd_det.schd_time      = act_schd.schd_time
                    and schd_det.schd_interval  = act_schd.schd_interval
                    and schd_det.schd_reference = act_schd.schd_reference
                  exclusive-lock no-error.

               /*OVERLAPPING REQUIREMENT*/
               if available schd_det then
               do:

/*K229*/          /* EXECUTE THE BELOW LOGIC FOR ALL NON-CUMULATIVE ORDERS */
/*K229*/          /* REGARDLESS OF SO_MERGE_RSS FLAG                       */

/*K229**          if so_merge_rss then */
/*K229**          do:                  */

                  assign
/*K1Y4**             act_prcnt_ship = integer((act_schd.schd_ship_qty / */
/*K1Y4**                              act_schd.schd_discr_qty) * 100)   */
                     new_prcnt_ship = integer((act_schd.schd_ship_qty /
                                      schd_det.schd_discr_qty) * 100).

/*K229*/          /* CHECK REQUIREMENT QUANTITY CHANGE, IF SO_MERGE_RSS. */
/*K229*/          /* CODE MOVED BELOW INSIDE SO_MERGE_RSS LOOP           */

/*K229*** BEGIN DELETE ***
 *                   /***********************************************/
 *                   /* CHECK IF REQUIREMENT QUANTITY HAS CHANGED.  */
 *                   /***********************************************/
 *                   if (schd_det.schd_discr_qty <>
 *                      act_schd.schd_discr_qty) then
 *                   do:
 *                      msgtext = string(schd_det.schd_date) + "/" +
 *                                   schd_det.schd_time + "/" +
 *                                   schd_det.schd_ref + "/" +
 *                                   string(act_schd.schd_discr_qty) + "/" +
 *                                   string(schd_det.schd_discr_qty).
 *
 *                      {gprun.i ""gpcfmsg.p"" "(input 1522,
 *                           input 0,
 *                           input program-name(1),
 *                           input so_site,
 *                           input so_ship,
 *                           input msgtext,
 *                           input true,
 *                           output errlevel)"}
 *
 *                      /* CHECK THE EFFECTS OF CHANGED REQ. QTY. */
 *                      {gprun.i ""rcrsupc1.p""
 *                         "(input recid(sod_det),
 *                         input schd_det.schd_discr_qty,
 *                         input act_schd.schd_discr_qty,
 *                         input act_schd.schd_all_qty,
 *                         input act_schd.schd_ship_qty,
 *                         output errgen)"}
 *
 *                      errgen = if errlevel > 2 then true else errgen.
 *
 *                      if errgen then
 *                         undo, return.
 *
 *                   end. /* REQUIREMENT CHANGED */
 *K229*** END DELETE ***/

                  if new_prcnt_ship >= ship_cmplt
                     and act_schd.schd_all_qty = 0 then
                  do:

/*K229*/             /* IF THE REQUIREMENT HAS BEEN FULLY SHIPPED AND   */
/*K229*/             /* THE QUANTITY ALLOCATED/PEGGED IS ZERO, DO NOT   */
/*K229*/             /* DELETE RQM_DET, SCHD_DET. INSTEAD COPY THE      */
/*K229*/             /* SHIP QTY FROM ACTIVE SCHEDULE ONTO NEW SCHEDULE */
/*K229*/             /* AND FLAG THIS SCHD_DET AS SHIPPED.              */
/*K229*/             /* i.e. SUBSTRING(SCHD_FC_QUAL,2,1) = "Y")         */

/*K229*** BEGIN DELETE ***
 *                   for each rqm_det
 *                      where rqm_type      = schd_det.schd_type
 *                        and rqm_nbr       = schd_det.schd_nbr
 *                        and rqm_line      = schd_det.schd_line
 *                        and rqm_rlse_id   = schd_det.schd_rlse_id
 *                        and rqm_date      = schd_det.schd_date
 *                        and rqm_time      = schd_det.schd_time
 *                        and rqm_interval  = schd_det.schd_interval
 *                        and rqm_reference = schd_det.schd_reference
 *                        exclusive-lock:
 *
 *                      delete rqm_det.
 *
 *                   end. /* for each rqm_det */
 *K229*** END DELETE ***/

/*K229*/             assign
/*K229*/                schd_det.schd_ship_qty = act_schd.schd_ship_qty
/*K229*/                substring(schd_det.schd_fc_qual,2,1) = "y".
/*K229**             delete schd_det. */

                     next.

                  end. /* if prcnt_ship >= ship_cmplt */

                  /************************************************/
                  /* REASSIGN UNCONFIRMED SHIPPER LINKS FROM      */
                  /* ACTIVE RSS TO THE NEW RSS.                   */
                  /************************************************/
                  if act_schd.schd_all_qty > 0 THEN
                  do:

/*K22Q*/ /* ORDER OF THE FIELDS HAS BEEN CHANGED TO MATCH THE NEW INDEX */
/*K22Q*/ /* CREATED IN THE SCHEMA.                                      */

                      for each absr_det
/*K22Q**                 where absr_type    = act_schd.schd_type        */
/*K22Q*/                 where absr_cnfrmd  = no
/*K22Q*/                 and absr_type      = act_schd.schd_type
                         and absr_nbr       = act_schd.schd_nbr
                         and absr_line      = act_schd.schd_line
                         and absr_rlse_id   = act_schd.schd_rlse_id
                         and absr_date      = act_schd.schd_date
                         and absr_time      = act_schd.schd_time
                         and absr_interval  = act_schd.schd_interval
                         and absr_reference = act_schd.schd_reference
/*K22Q**                 and not absr_cnfrmd                            */
                         exclusive-lock:

                         absr_rlse_id = new_rlse_id.
                     end.
                  end.

/*K229*/          /* PEGGED REQUIREMENTS NEED TO BE CONSIDERED IN THE   */
/*K229*/          /* NEW RSS REGARDLESS OF SO_MERGE_RSS FLAG.           */
/*K229*/          /* CODE MOVED FROM BELOW                              */

/*K229*/          assign
/*K229*/             schd_det.schd_ship_qty = act_schd.schd_ship_qty
/*K229*/             schd_det.schd_all_qty  = act_schd.schd_all_qty
/*K229*/             act_schd.schd_all_qty  = 0.

/*K229*/          if so_merge_rss then do:

/*K229*/             /* CHANGE REQUIREMENT QUANTITY CHECK MOVED FROM ABOVE, */
/*K229*/             /* SINCE IT HAS TO BE EXECUTED ONLY WHEN MERGE RSS IS  */
/*K229*/             /* YES.                                                */

/*K229*/             /***********************************************/
/*K229*/             /* CHECK IF REQUIREMENT QUANTITY HAS CHANGED.  */
/*K229*/             /***********************************************/

/*K229*/             if (schd_det.schd_discr_qty <>
/*K229*/                act_schd.schd_discr_qty) then
/*K229*/             do:
/*K229*/                msgtext = string(schd_det.schd_date) + "/" +
/*K229*/                             schd_det.schd_time + "/" +
/*K229*/                             schd_det.schd_reference + "/" +
/*K229*/                             string(act_schd.schd_discr_qty) + "/" +
/*K229*/                             string(schd_det.schd_discr_qty).

/*K229*/                {gprun.i ""gpcfmsg.p"" "(input 1522,
                             input 0,
                             input program-name(1),
                             input so_site,
                             input so_ship,
                             input msgtext,
                             input true,
                             output errlevel)"}
/*GUI*/ if global-beam-me-up then undo, leave.


/*K229*/                /* CHECK THE EFFECTS OF CHANGED REQ. QTY. */
/*K229*/                {gprun.i ""rcrsupc1.p""
                           "(input recid(sod_det),
                           input schd_det.schd_discr_qty,
                           input act_schd.schd_discr_qty,
                           input act_schd.schd_all_qty,
                           input act_schd.schd_ship_qty,
                           output errgen)"}
/*GUI*/ if global-beam-me-up then undo, leave.


/*K229*/                errgen = if errlevel > 2 then true else errgen.

/*K229*/                if errgen then
/*K229*/                   undo, return.

/*K229*/             end. /* REQUIREMENT CHANGED */

                     /***********************************************/
                     /* COPY ACTIVE RQM_DET RECORDS THAT DO NOT     */
                     /* EXIST IN NEW RSS TO NEW RSS.                */
                     /***********************************************/
                     for each act_rqm
                        where act_rqm.rqm_type      = act_schd.schd_type
                          and act_rqm.rqm_nbr       = act_schd.schd_nbr
                          and act_rqm.rqm_line      = act_schd.schd_line
                          and act_rqm.rqm_rlse_id   = act_schd.schd_rlse_id
                          and act_rqm.rqm_date      = act_schd.schd_date
                          and act_rqm.rqm_time      = act_schd.schd_time
                          and act_rqm.rqm_interval  = act_schd.schd_interval
                          and act_rqm.rqm_reference = act_schd.schd_reference
                        no-lock:

                        find first rqm_det
                           where rqm_det.rqm_type       = act_rqm.rqm_type
                             and rqm_det.rqm_nbr        = act_rqm.rqm_nbr
                             and rqm_det.rqm_line       = act_rqm.rqm_line
                             and rqm_det.rqm_rlse_id    = new_rlse_id
                             and rqm_det.rqm_date       = act_rqm.rqm_date
                             and rqm_det.rqm_time       = act_rqm.rqm_time
                             and rqm_det.rqm_interval   = act_rqm.rqm_interval
                             and rqm_det.rqm_reference  = act_rqm.rqm_reference
                             and rqm_det.rqm_cat        = act_rqm.rqm_cat
                             and rqm_det.rqm_value      = act_rqm.rqm_value
                           no-lock no-error.

                        if not available rqm_det then
                        do:
                           create rqm_det.
                           assign
                              rqm_det.rqm_type      = act_rqm.rqm_type
                              rqm_det.rqm_nbr       = act_rqm.rqm_nbr
                              rqm_det.rqm_line      = act_rqm.rqm_line
                              rqm_det.rqm_rlse_id   = new_rlse_id
                              rqm_det.rqm_date      = act_rqm.rqm_date
                              rqm_det.rqm_time      = act_rqm.rqm_time
                              rqm_det.rqm_interval  = act_rqm.rqm_interval
                              rqm_det.rqm_reference = act_rqm.rqm_reference
                              rqm_det.rqm_cat       = act_rqm.rqm_cat
                              rqm_det.rqm_value     = act_rqm.rqm_value.
                        end. /* if not available rqm_det */
                     end. /* for each act_rqm */

/*K229*/             /* PEGGED REQUIREMENTS NEED TO BE CONSIDERED IN THE   */
/*K229*/             /* NEW RSS REGARDLESS OF SO_MERGE_RSS FLAG.           */
/*K229*/             /* CODE MOVED ABOVE OUTSIDE SO_MERGE_RSS LOOP         */

/*K229**             assign */
/*K229**                schd_det.schd_ship_qty = act_schd.schd_ship_qty */
/*K229**                schd_det.schd_all_qty  = act_schd.schd_all_qty  */
/*K229**                act_schd.schd_all_qty  = 0. */

                  end. /* if so_merge_rss then */
               end.  /* END OVERLAPPING REQUIREMENTS */


               /*****************************************************/
               /* IF AN OPEN REQUIREMENT IS IN THE ACTIVE RSS BUT   */
               /* NOT THE NEW, COPY TO THE NEW RSS.                 */
               /*****************************************************/
               else
               do:
                  if (act_schd.schd_all_qty > 0)
                     or (so_merge_rss
/*K1Y4**                and new_prcnt_ship < ship_cmplt) then */
/*K1Y4*/                and act_prcnt_ship < ship_cmplt) then
                  do:

                     /***********************************************/
                     /* COPY ACTIVE SCHD_DET TO NEW SCHD_DET.       */
                     /***********************************************/
                     create schd_det.
                     assign
                        schd_det.schd_type      = act_schd.schd_type
                        schd_det.schd_nbr       = act_schd.schd_nbr
                        schd_det.schd_line      = act_schd.schd_line
                        schd_det.schd_rlse_id   = new_rlse_id
                        schd_det.schd_date      = act_schd.schd_date
                        schd_det.schd_time      = act_schd.schd_time
                        schd_det.schd_interval  = act_schd.schd_interval
                        schd_det.schd_reference = act_schd.schd_reference.

                     if recid(schd_det) = -1 then.
                     /***********************************************/
                     /* COPY REMAINING SCHD_DET COLUMNS.            */
                     /***********************************************/
                     {gprun.i ""rcsdinit.p"" "(input recid(act_schd),
                                               input recid(schd_det))"}
/*GUI*/ if global-beam-me-up then undo, leave.


                     /***********************************************/
                     /* REASSIGN UNCONFIRMED SHIPPER LINKS FROM     */
                     /* ACTIVE RSS TO NEW RSS.                      */
                     /***********************************************/

                     for each absr_det
/*K22Q*                  where absr_type     = act_schd.schd_type      */
/*K22Q*/                 where absr_cnfrmd   = no
/*K22Q*/                  and absr_type      = act_schd.schd_type
                          and absr_nbr       = act_schd.schd_nbr
                          and absr_line      = act_schd.schd_line
                          and absr_rlse_id    = act_schd.schd_rlse_id
                          and absr_date      = act_schd.schd_date
                          and absr_time      = act_schd.schd_time
                          and absr_interval  = act_schd.schd_interval
                          and absr_reference = act_schd.schd_reference
/*K22Q**                  and not absr_cnfrmd                          */
                        exclusive-lock:

                        absr_rlse_id = new_rlse_id.
                     end.

                     /***********************************************/
                     /* COPY RQM_DET FROM ACTIVE RSS TO NEW RSS.    */
                     /***********************************************/
                      if can-find(first rqm_det
                        where rqm_type      = act_schd.schd_type
                          and rqm_nbr       = act_schd.schd_nbr
                          and rqm_line      = act_schd.schd_line
                          and rqm_rlse_id   = act_schd.schd_rlse_id
                          and rqm_date      = act_schd.schd_date
                          and rqm_time      = act_schd.schd_time
                          and rqm_interval  = act_schd.schd_interval
                          and rqm_reference = act_schd.schd_reference) then
                     do:

                        {gprun.i ""rcsinita.p"" "(input recid(schd_det),
                                                  input act_schd.schd_rlse_id)"}
/*GUI*/ if global-beam-me-up then undo, leave.


                     end. /* if can-find */

                  end. /* if (act_schd.schd_all_qty > 0) */

               end.  /* if avail schd_det */

            end.
/*GUI*/ if global-beam-me-up then undo, leave.
  /* for each act_schd*/

         end. /* if not so_cum_acct */

         undo_status = false.
